import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/apis.dart';
import 'package:hbtrans_flutter/beans/traintype_bean.dart';
import 'package:hbtrans_flutter/const.dart';
import 'package:hbtrans_flutter/event/TrainTypeEvent.dart';
import 'package:hbtrans_flutter/routes.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';

class TrainTypePage extends StatefulWidget {
  final inscode;

//  const TrainTypePage({Key key}) : super(key: key);
   TrainTypePage(this.inscode);

  @override
  _TrainTypePageState createState() => _TrainTypePageState();
}

class _TrainTypePageState extends State<TrainTypePage> {
  var name = '';
  var price = '';
  var type = '';

  Map<String, dynamic> data = Map();
  var lists = List();
  bool isCheck = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    traintype();
  }

  void traintype() async {
    MyDio.getInstance().clearInstance();
//    data['inscode'] = '1475137653436511';
    data['inscode'] = widget.inscode;
    MyDio.getInstance(url: Const.TRANS_SERVER_PATH_NEW3).get(Apis.tarintype,
        (data) {
      print(data);

      setState(() {
        lists.clear();
        for (var s in data) {
          lists.add(TraintypeBean.fromJson(s));
        }
      });
    }, params: data);
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(18, 1, 18, 0),
      child: _doWidgets(),
    );
  }

  _listItem() {
    List<Widget> widget = List();
    for (int i = 0; i < lists.length; i++) {
      widget.add(GestureDetector(
        onTap: () {
          eventBus.fire(TrainTypeEvent(lists[i].id, lists[i].name));
//          setState(() {
//            isCheck = !isCheck;
//          });
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.fromLTRB(0, 16, 0, 5),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Text(
                        lists[i].name,
                        style: TextStyle(color: Colors.black, fontSize: 16),
                      ),
                      Text(
                        lists[i].price,
                        style: TextStyle(color: Colors.orange, fontSize: 14),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 3,
                  ),
                  Text(
                    "全费    教学类型：" + lists[i].classtype,
                    style: TextStyle(color: Colors.black54, fontSize: 14),
                  ),
                ],
              ),
            ),
            Padding(
                padding: EdgeInsets.all(10),
                child: isCheck
                    ? Image.asset(
                        'images/ic_select.png',
                        width: 22,
                        height: 22,
                      )
                    : Image.asset(
                        'images/ic_unselect.png',
                        width: 22,
                        height: 22,
                      )),
          ],
        ),
      ));
    }
    return widget;
  }

  _doWidgets() {
    return Center(
        child: lists.length == 0
            ? Column(
                children: <Widget>[
                  Image.asset(
                    'images/ic_school_null.png',
                    fit: BoxFit.fill,
                  ),
                  SizedBox(
                    height: 15,
                  ),
                  Text(
                    '暂未开通',
                    style: TextStyle(color: Color(0xff999999), fontSize: 14),
                  ),
                ],
              )
            : ListView(
                children: _listItem(),
              )
//        ListView.builder(
//          shrinkWrap: false,
//          itemCount: lists.length,
//          physics: NeverScrollableScrollPhysics(),
//          itemBuilder: (BuildContext context, int index) {
//            return _listItem(index, lists[index]);
//          },
//        )
        );
  }
}
